﻿/////////////////////////////////////////////////////////////////////////////////
//      OpenDNS Updater
//      Copyright (C) 2008 Jason Strese
//
//      This program is free software: you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation, either version 3 of the License, or
//      (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program.  If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OpenDNSUpdater
{
    public partial class frmOptions : Form
    {
        public frmOptions()
        {
            InitializeComponent();
        }

        //
        // Button: Default (for URL)
        // Sets the text-box for the Update URL to the default URL
        //
        private void button1_Click(object sender, EventArgs e)
        {
            this.txtURL.Text = "https://updates.opendns.com/account/ddns.php?";
        }

        //
        // Button: Accept
        // Closes the form and saves the text-box value to OPEN_URL
        //
        private void button2_Click(object sender, EventArgs e)
        {
            //
            // The URL must start with "http://" or "https://"
            //
            if (!this.txtURL.Text.StartsWith("http://") && !this.txtURL.Text.StartsWith("https://"))
            {
                MessageBox.Show(this, "The update URL must start with either 'http://' or 'https://'.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }

            //
            // The URL must be greater than 12 characters in length (at MINIMUM)
            //
            if (this.txtURL.Text.Length < 12)
            {
                MessageBox.Show(this, "The update URL must be greater than or equal to 12 characters in length.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }

            //
            // If the afforementioned conditions didn't return, then this is a (for the most part) valid URL
            // Now we will save it and close this window and save the URL
            //
            OpenDNSUpdater.Properties.Settings.Default.OPEN_URL = this.txtURL.Text;

            //
            // Save value of Interval
            //
            switch (this.comboBox1.SelectedIndex)
            {
                default:
                case 0: // 1 hour
                    OpenDNSUpdater.Properties.Settings.Default.OPEN_INTERVAL = 3600000;
                    break;
                case 1: // 3 hours
                    OpenDNSUpdater.Properties.Settings.Default.OPEN_INTERVAL = 10800000;
                    break;
                case 2: // 5 hours
                    OpenDNSUpdater.Properties.Settings.Default.OPEN_INTERVAL = 18000000;
                    break;
                case 3: // 7 hours
                    OpenDNSUpdater.Properties.Settings.Default.OPEN_INTERVAL = 25200000;
                    break;
                case 4: // 1 day
                    OpenDNSUpdater.Properties.Settings.Default.OPEN_INTERVAL = 86400000;
                    break;
            }

            this.Close();
        }

        //
        // Button: Cancel
        // Close the form
        //
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //
        // Form Load
        // Set the options values to the currently set values
        //
        private void frmOptions_Load(object sender, EventArgs e)
        {
            // URL
            this.txtURL.Text = OpenDNSUpdater.Properties.Settings.Default.OPEN_URL;

            // Interval
            switch (OpenDNSUpdater.Properties.Settings.Default.OPEN_INTERVAL)
            {
                default:
                case 3600000: // 1 hour
                    this.comboBox1.SelectedIndex = 0;
                    break;
                case 10800000: // 3 hours
                    this.comboBox1.SelectedIndex = 1;
                    break;
                case 18000000: // 5 hours
                    this.comboBox1.SelectedIndex = 2;
                    break;
                case 25200000: // 7 hours
                    this.comboBox1.SelectedIndex = 3;
                    break;
                case 86400000: // 1 day
                    this.comboBox1.SelectedIndex = 4;
                    break;
            }
        }

        //
        // Button: Default (for Interval)
        //
        private void button4_Click(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
        }
    }
}
